package cn.kgc.springbootvuecontact.service;

import cn.kgc.springbootvuecontact.mapper.ContactMapper;
import cn.kgc.springbootvuecontact.pojo.Contact;
import cn.kgc.springbootvuecontact.pojo.ContactExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ContactServiceImpl implements ContactService{
    @Resource
    ContactMapper mapper;
    //分页查询联系人
    @Override
    public PageInfo<Contact> getSelectBycname(String cname, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        ContactExample example=new ContactExample();
        ContactExample.Criteria criteria=example.createCriteria();
        if(cname!=null&&cname!=""){
            criteria.andCnameLike("%"+cname+"%");
            List<Contact> list=mapper.selectByExample(example);
            PageInfo<Contact> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }else{
            List<Contact> list=mapper.selectByExample(null);
            PageInfo<Contact> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }

    }
    //根据id查询联系人
    @Override
    public Contact getSelectByid(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }
    //修改联系人
    @Override
    public int Update(Contact contact) {
        return mapper.updateByPrimaryKeySelective(contact);
    }
    //添加联系人
    @Override
    public int Add(Contact contact) {
        return mapper.insertSelective(contact);
    }

}
